﻿
import { Dialog } from 'primeng/components/dialog/dialog';
import { Component, Injector, ViewChild, OnInit } from '@angular/core';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { AppComponentBase } from '@shared/common/app-component-base';
import { RoleListDto, RoleServiceProxy, ContactDto, ContactServiceProxy } from '@shared/service-proxies/service-proxies';
import { Table } from 'primeng/components/table/table';
import { EntityTypeHistoryModalComponent } from '@app/shared/common/entityHistory/entity-type-history-modal.component';
import * as _ from 'lodash';
import { finalize } from 'rxjs/operators';


@Component({
    selector : 'app-contact',
    templateUrl: './contact.component.html',
    styleUrls: ['./contact.component.less'],
    animations: [appModuleAnimation()]
})
export class ContactComponent extends AppComponentBase implements OnInit  {

    @ViewChild('dt') dataTable: Table;

    displayDialog: boolean;

    contact: ContactDto;
    
    selectedContact: ContactDto;
    
    newContact: boolean;
    
    contacts: ContactDto[];
    constructor(
        injector: Injector,
        private _contactServiceProxy: ContactServiceProxy
        ) 
        {
            super(injector);
            
        }

    
    
    cols: any[];
    ngOnInit(): void {
            this._contactServiceProxy.getAllContacts('')
            .subscribe(result => {
                this.contacts = result;
              });

            this.cols = [
                { field: 'id', header: '编号' },
                { field: 'companyName', header: '单位名称' },
                { field: 'contacts', header: '联系人' },
                { field: 'telephone', header: '联系电话' },
                { field: 'address', header: '地址' }
            ];
    }

    showDialogToAdd() {
        this.newContact = true;
        this.contact = new ContactDto();
        this.displayDialog = true;
    }


    save() {
        this._contactServiceProxy.createOrUpdateContact(this.contact)
        .subscribe(result => {
                              
            if (this.newContact){
                
                this.contact.id = result;      
                this.contacts.push(this.contact);   
            } 
            else
                this.contacts[this.contacts.indexOf(this.selectedContact)] = this.contact;

            this.contact = null;
            this.displayDialog = false;
          });
        
    }

    delete() {
        let index = this.contacts.indexOf(this.selectedContact);
        this._contactServiceProxy.deleteContact(this.contact.id)
        .subscribe(result => {
            this.contacts = this.contacts.filter((val, i) => i != index);
          });
        
        this.contact = null;
        this.displayDialog = false;
    }

    onRowSelect(event) {
        this.newContact = false;
        this.contact = this.cloneContact(event.data);
        this.displayDialog = true;
    }

    cloneContact(c: ContactDto): ContactDto {
        let contact = new ContactDto();
        for (let prop in c) {
            contact[prop] = c[prop];
        }
        return contact;
    }
}
